package com.twu.bm.football;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class FootballLeagueAppTest {

    PrintStream printStream = mock(PrintStream.class);
    BufferedReader reader = mock(BufferedReader.class);

    ListOption listOption = mock(ListOption.class);
    SearchOption searchOption = mock(SearchOption.class);
    TeamListOption teamListOption = mock(TeamListOption.class);
    Map<String, MenuOption> options = new HashMap<String, MenuOption>();

    FootballLeagueApp footballLeagueApp = new FootballLeagueApp(printStream, reader, options);

    @Before
    public void setUpOptionsList() {
        options.put("1", listOption);
        options.put("2", searchOption);
        options.put("3", teamListOption);
    }

    @Test
    public void shouldPrintAllOptionsWhenAppRuns() throws IOException {
        when (reader.readLine()).thenReturn("0");

        footballLeagueApp.run();
        verify(printStream).println("Enter a number to select an option:");
        verify(printStream).println("1 - List players and coaches");
        verify(printStream).println("2 - Search for a player");
        verify(printStream).println("3 - See members of team");
    }

    @Test (expected =  IOException.class)
    public void shouldThrowIOExceptionWhenReaderGetsIOException() throws IOException {
        when(reader.readLine()).thenThrow(new IOException());
        footballLeagueApp.run();
    }

    @Test
    public void shouldPrintErrorMessageWhenInvalidInputIsEntered() throws IOException {
        when(reader.readLine()).thenReturn("0");
        footballLeagueApp.run();
        verify(printStream).println("Invalid input!");
    }

    @Test
    public void shouldPrintLeagueWhenOneIsEntered() throws IOException {
        when (reader.readLine()).thenReturn("1");
        footballLeagueApp.run();
        verify(listOption).performAction();
    }

    @Test
    public void shouldStartSearchWhenTwoIsEntered() throws IOException {
        when(reader.readLine()).thenReturn("2");
        footballLeagueApp.run();
        verify(searchOption).performAction();
    }

    @Test
    public void shouldCallTeamListWhenThreeIsEntered() throws IOException {
        when(reader.readLine()).thenReturn("3");
        footballLeagueApp.run();
        verify(teamListOption).performAction();
    }

}